"""
Case Type   : HTAP行列融合特性
Case Name   : max_process_memory取值较小，max_imcs_cache值较大，大数据量表进行转换
Create At   : 2024/12/25
Owner       : ningyali
Description :
    1.设置max_process_memory值
    2.设置max_imcs_cache值
    3.建表插入大量数据
    4.行列转换
    5.清理数据环境
Expect      :
    1.执行成功
    2.执行成功
    3.执行成功
    4.执行成功
    5.执行成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro


class HTAP(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('SingleDbUser')
        self.Constant = Constant()
        self.common = Common()
        self.tmp_path = os.path.join(macro.DB_INSTANCE_PATH, 'base', 'imcu')

    def test_trans(self):
        text = '----step1:设置max_process_memory值;expect:执行成功----'
        self.log.info(text)
        guc_res = self.pri_sh.execute_gsguc(
            'set', self.Constant.GSGUC_SUCCESS_MSG,
            f'max_process_memory=2048MB')
        self.assertEqual(True, guc_res, f'参数配置失败:{text}')

        text = '----step2:设置max_imcs_cache值;expect:执行成功----'
        self.log.info(text)
        guc_res = self.pri_sh.execute_gsguc(
            'set', self.Constant.GSGUC_SUCCESS_MSG,
            f'max_imcs_cache=2000MB')
        self.assertEqual(True, guc_res, f'参数配置失败:{text}')

        self.log.info('重启数据库使设置生效')
        is_restarted = self.pri_sh.restart_db_cluster()
        self.assertTrue(is_restarted, f'重启数据库失败:{text}')

        text = '----step3:建表插入大量数据;expect:执行成功----'
        self.log.info(text)
        sql = 'drop table if exists t_htap_single_0119;' \
              'create table t_htap_single_0119(' \
              'c1 integer,' \
              'c2 varchar(30),' \
              'c3 char(30));' \
              'insert into t_htap_single_0119 values (' \
              'generate_series(1, 35000000),' \
              'generate_series(1, 35000000)||\'aaa\',' \
              'generate_series(1, 35000000)||\'bbb\');' \
              '\\d+'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        expect = r'CREATE TABLE\s+INSERT 0 35000000'
        self.assertRegex(sql_res, expect, f'执行失败:{text}')

        text = '----step4:行列转换;expect:执行成功----'
        self.log.info(text)
        sql = 'alter table t_htap_single_0119 imcstored;' \
              'select * from  query_imcstore_views();' \
              'set enable_imcsscan to on;' \
              'select count(c1) from t_htap_single_0119 where c1%2=0;'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertEqual(sql_res.count('ALTER TABLE'), 1, f'执行失败:{text}')
        self.assertEqual(sql_res.count('17500000'), 1, f'执行失败:{text}')
        self.assertIn('SET', sql_res, f'执行失败:{text}')

        free_cmd = f"free -m|grep 'Mem'|awk '{{{{print $4}}}}';"
        self.log.info(free_cmd)
        free_res1 = self.pri_sh.node.sh(free_cmd).result()
        self.log.info(free_res1)

        ls_cmd = f"ls {self.tmp_path}|wc -l;"
        self.log.info(ls_cmd)
        ls_res = self.pri_sh.node.sh(ls_cmd).result()
        self.log.info(ls_res)
        self.assertGreater(int(ls_res.strip()), 0, f'执行失败:{text}')

        text = '----step5:取消行列转换;expect:执行成功----'
        self.log.info(text)
        sql = 'alter table t_htap_single_0119 unimcstored;'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertEqual(sql_res.count('ALTER TABLE'), 1, f'执行失败:{text}')

        free_cmd = f"free -m|grep 'Mem'|awk '{{{{print $4}}}}';"
        self.log.info(free_cmd)
        free_res2 = self.pri_sh.node.sh(free_cmd).result()
        self.log.info(free_res2)
        self.assertGreater(int(free_res2.strip()), int(free_res1.strip()),
                           f'执行失败:{text}')

    def tearDown(self):
        text = '----step6:清理环境;expect:执行成功----'
        self.log.info(text)
        sql = 'drop table if exists t_htap_single_0119;'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)

        guc_res1 = self.pri_sh.execute_gsguc(
            'set', self.Constant.GSGUC_SUCCESS_MSG,
            f'max_process_memory')
        guc_res2 = self.pri_sh.execute_gsguc(
            'set', self.Constant.GSGUC_SUCCESS_MSG,
            f'max_imcs_cache')
        is_restarted = self.pri_sh.restart_db_cluster()

        self.assertIn('DROP TABLE', sql_res, f'删除表失败:{text}')
        self.assertTrue(guc_res1, f'参数配置失败:{text}')
        self.assertTrue(guc_res2, f'参数配置失败:{text}')
        self.assertTrue(is_restarted, f'重启数据库失败:{text}')

        self.log.info(f'-----{os.path.basename(__file__)} end-----')
